package com.raja.servingStation.dao;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.datasources.RepairSql2oTemp;
import com.raja.servingStation.entity.KmAndMonth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.sql2o.Connection;
import org.sql2o.Query;

import java.util.List;


/**
 * 索赔Dao
 * on 2017/9/30.
 */
@Repository
public class ClaimDao {
    private static final Logger log = LoggerFactory.getLogger(ClaimDao.class);

    @Autowired
    private RepairSql2oTemp sql2oTemp;


    public Object claimSbOrderByOrderId(JSONObject body){
        String sql = "insert into t_order_claim ( ORDER_ID,CLAIM_STATE,CLAIM_TIME) " +
                "    values (:orderId,0,now())" ;
        log.debug(sql);
        try (Connection con = sql2oTemp.getSql2o().open()) {
            Query query = con.createQuery(sql);
            Query query2 = con.createQuery("select max(CLAIM_ID) from t_order_claim ");
            query.addParameter("orderId", body.getString("orderId"));
            query.executeUpdate();
            return query2.executeScalar();
        }
    }


    public Object claimRepairOrderByOrderId(JSONObject body){
        String sql = "insert into t_order_claim ( ORDER_ID,CLAIM_STATE,CLAIM_TIME) " +
                "    values (:orderId,0,now())" ;
        log.debug(sql);
        try (Connection con = sql2oTemp.getSql2o().open()) {
            Query query = con.createQuery(sql);
            Query query2 = con.createQuery("select max(CLAIM_ID) from t_order_claim ");
            query.addParameter("orderId", body.getString("orderId"));
            query.executeUpdate();
            return query2.executeScalar();
        }
    }

    public Integer getBlameType(JSONObject body){
        String sql = "select BLAME_TYPE from T_FAULT_DIACRISIS where ORDER_ID = :orderId";
        log.debug(sql);
        try (Connection con = sql2oTemp.getSql2o().open()) {
            Query query = con.createQuery(sql);
            query.addParameter("orderId", body.getString("orderId"));
            return (Integer) query.executeScalar();
        }
    }

    public Integer getClaimState(JSONObject body){
        String sql = "select CLAIM_STATE from t_order_claim where ORDER_ID = :orderId";
        log.debug(sql);
        try (Connection con = sql2oTemp.getSql2o().open()) {
            Query query = con.createQuery(sql);
            query.addParameter("orderId", body.getString("orderId"));
            return (Integer) query.executeScalar();
        }
    }

    public KmAndMonth getCarInfo(JSONObject body){
        String sql = "select c.TOTAL_KM km,PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(c.KP_TIME,'%Y%m')) month \n" +
                "        from T_REPAIR_ORDER o\n" +
                "        INNER JOIN t_car c on o.CAR_ID = c.id\n" +
                "        WHERE o.ORDER_ID = :orderId";
        log.debug(sql);
        try (Connection con = sql2oTemp.getSql2o().open()) {
            Query query = con.createQuery(sql);
            query.addParameter("orderId", body.getString("orderId"));
            return query.executeAndFetchFirst(KmAndMonth.class);
        }
    }

    public List<KmAndMonth> getBxInfo(JSONObject body){
        String sql = "select cm.GUARANTEE_MONTH km, cm.GUARANTEE_MILEAGE month\n" +
                "         from (select * from T_REPAIR_PLAN where PRICE_TYPE=1) plan\n" +
                "         LEFT JOIN T_CAR_COMPONENT cmpt on plan.FK_ID = cmpt.DP_ID\n" +
                "         LEFT JOIN t_component_model cm on cmpt.DP_ID = cm.DP_ID\n" +
                "         where plan.ORDER_ID = :orderId ";
        log.debug(sql);
        try (Connection con = sql2oTemp.getSql2o().open()) {
            Query query = con.createQuery(sql);
            query.addParameter("orderId", body.getString("orderId"));
            return query.setAutoDeriveColumnNames(true).executeAndFetch(KmAndMonth.class);
        }
    }


}
